<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\DbConnection\Model\Model;
/**
 * @property int $id
 * @property string $oaid
 * @property string $imei
 * @property int $pay_amount
 * @property string $pkg
 * @property string $res
 * @property int $type
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 */
class OcpcVivoLog extends Model
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'ocpc_vivo_log';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['id', 'oaid', 'imei', 'pay_amount', 'pkg', 'res', 'type', 'created_at', 'updated_at','user_id','is_register'];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'integer', 'pay_amount' => 'integer', 'type' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime','user_id'=>'integer','is_register'=>'integer'];


    public const CHANNELS = [503];


    //当天注册人数
    public static function getRegisterUser($startDate,$endDate,$package){
        $num = self::select(['user_id'])->where([
            ['created_at','>=',$startDate],
            ['created_at','<=',$endDate],
            ['pkg','=',$package],
            ['type','=',2],
        ])->groupBy(['user_id'])->get()->toArray();
        return count($num);
    }

    //当天充值人数
    public static function getRechargeUser($startDate,$endDate,$package,$isReg=false){

        $where = [
            ['created_at','>=',$startDate],
            ['created_at','<=',$endDate],
            ['pkg','=',$package],
            ['type','=',3],
        ];

        if($isReg){
            $where[] = ['is_register','=',1];
        }

        $num = self::select(['user_id'])->where($where)->groupBy(['user_id'])->get()->toArray();
        return count($num);
    }

    //当天充值金额 分
    public static function getRechargeMoney($startDate,$endDate,$package,$isReg=false){

        $where = [
            ['created_at','>=',$startDate],
            ['created_at','<=',$endDate],
            ['pkg','=',$package],
            ['type','=',3],
        ];

        if($isReg){
            $where[] = ['is_register','=',1];
        }

        $num = self::where($where)->sum('pay_amount');
        return $num;
    }

    //当天充值次数
    public static function getRechargeNum($startDate,$endDate,$package,$isReg=false){

        $where = [
            ['created_at','>=',$startDate],
            ['created_at','<=',$endDate],
            ['type','=',3],
            ['pkg','=',$package],
        ];

        if($isReg){
            $where[] = ['is_register','=',1];
        }
        $num = self::where($where)->count();
        return $num;
    }

}